/*
 * @Description: 添加散文件和档案类型弹窗
 * @Author: Zzt
 * @Date: 2022-11-03 16:32:50
 * @LastEditTime: 2023-01-30 15:35:50
 */
import { appState } from '@/store/mobx';
import { jsonClone } from '@/utils';
import { Button, Form, Modal } from 'antd';
import { useEffect, useState } from 'react';
import { useMappingRelationContext } from '../../../contexts/MappingRelationContext';
import FATForm from './FATForm';
const EditFAT = (props: any) => {
  const { visible, onClose, data, type } = props;
  const { editFAT } = useMappingRelationContext();

  console.log('datadadadata', data);
  const [selectUnit, setSelectUnit] = useState<string | number>();
  const [initTable, setInitTable] = useState([]);
  const [form] = Form.useForm();
  const [countid, setCountid] = useState(1);

  useEffect(() => {
    form.setFieldsValue({ ...data });
    setInitTable(data?.initTable);
    setSelectUnit(appState.unitId);
  }, [visible]);
  const onFinish = (value: any) => {
    editFAT({ ...data, ...value, initTable, id: countid }, type);
    setCountid((state) => state + 1);
    setInitTable([]);
    onClose();
    console.log('initTable', initTable, value);
  };
  const onChange = (description: any, allValues: any) => {
    console.log(description, allValues);
  };
  return (
    <>
      <Modal
        title={type === 1 ? '散文件与档案类型的映射关系' : '档案类型与散文件的映射关系'}
        width={750}
        visible={visible}
        onCancel={onClose}
        footer={[
          <Button key="cancel" onClick={onClose}>
            取消
          </Button>,
          <Button key="submit" type="primary" onClick={() => form.submit()}>
            确定
          </Button>
        ]}
      >
        <Form form={form} onFinish={onFinish} onValuesChange={onChange}>
          <FATForm
            form={form}
            setInitTable={setInitTable}
            initTable={initTable}
            data={data}
            selectUnit={selectUnit}
            onClose={onClose}
            FATOrTAF={type}
          />
        </Form>
      </Modal>
    </>
  );
};
export default EditFAT;
